//
//  LCYHomePageController.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 liu. All rights reserved.
//

#import "LCYHomePageController.h"
#import "CK_MenuViewController.h"
#import "LCYNetWorkRequestManager.h"
#import "LCYCollectionViewCell.h"
#import "LCYHomeModel.h"
#import "LYCycleView.h"
#import "LCYHomeDetailController.h"

/// 跳转页面
#import "LCYRadionTableViewController.h"
#import "LCYProductController.h"
#import "LCYTopicController.h"
#import "LCYLoginViewController.h"
#import "LCYUserInfoManager.h"

@interface LCYHomePageController ()<MenuClickDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

/// collectionView
@property (nonatomic,strong) UICollectionView *collectionView;
/// 数组属性
@property (nonatomic,strong) NSMutableArray *dataSource;
/// 轮播图属性
@property (nonatomic,strong) LYCycleView *cycleView;

@end

@implementation LCYHomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [CK_MenuViewController getMenuViewController].menuDelegate = self;
    
    
//    if (![LCYUserInfoManager shareManager].uid) {
//        [CK_MenuViewController getMenuViewController].menuArray = [@[[LCYUserInfoManager shareManager].uname,@"今日・TODAY",@"电台・RADIO",@"话题・TOPICS",@"良品・PRODECTS"] mutableCopy];
//    } else {
        [CK_MenuViewController getMenuViewController].menuArray = [@[@"",@"今日・TODAY",@"电台・RADIO",@"话题・TOPICS",@"良品・PRODECTS"] mutableCopy];
//    }
    
    /// 初始化数组
    self.dataSource = [NSMutableArray new];
    
    // 创建collectionView及轮播图
    [self createCollectionView];
    
    // 调用网络数据
    [self readUrl];
}
#pragma mark - 创建collectionView
- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    // 设置layout
    layout.itemSize = CGSizeMake(self.view.frame.size.width * 0.3, self.view.frame.size.height * 0.2);
    [self.view addSubview:self.collectionView];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    // 设置代理
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 200);
    self.cycleView = [[LYCycleView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) InterVal:2];
    
    // 注册
    [self.collectionView registerNib:[UINib nibWithNibName:@"LCYCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell"];
    /// 注册头部
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}

#pragma mark - 读取网络数据
///读取网络数据
- (void)readUrl {
    [LCYNetWorkRequestManager requestURL:[NSURL URLWithString:READLIST_URL] RequestMethod:LCY_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
//        NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        if (data) {
            NSMutableDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDic = jsonDic[@"data"];
            NSArray *listArray = dataDic[@"list"];
            for (NSDictionary *dic in listArray) {
                LCYHomeModel *model = [LCYHomeModel new];
                [model setValuesForKeysWithDictionary:dic];
                [self.dataSource addObject:model];
            }
        }
        NSLog(@"%ld",self.dataSource.count);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
            self.cycleView.imageNameArray = [@[@"read1.jpg",@"read2.jpg"] mutableCopy];
        });
        
    }];
}
#pragma mark - 点击抽屉按钮
- (IBAction)didClickLeftBarBtnItem:(UIBarButtonItem *)sender {
    if ([CK_MenuViewController getMenuViewController].isShowing) {
        [[CK_MenuViewController getMenuViewController] hideLeftViewController];
    } else {
        [[CK_MenuViewController getMenuViewController] showLeftViewController];
    }
}

#pragma mark - 点击菜单项的代理
- (void)didClickMenuIndex:(NSInteger)index Title:(NSString *)title {
//    [self.navigationController popToRootViewControllerAnimated:NO];
    switch (index) {
        case 0:
        {
            LCYLoginViewController *loginVC = [[LCYLoginViewController alloc]init];
            UINavigationController *navc = [[UINavigationController alloc]initWithRootViewController:loginVC];
            [self presentViewController:navc animated:YES completion:^{
                
            }];
        }
            break;
        case 2:
        {
            LCYRadionTableViewController *radioVC = [[LCYRadionTableViewController alloc]init];
            radioVC.title = title;
            [self.navigationController pushViewController:radioVC animated:YES];
        }
            break;
        case 3:
        {
            LCYTopicController *TopicTVC = [[LCYTopicController alloc]init];
            TopicTVC.title = title;
            [self.navigationController pushViewController:TopicTVC animated:YES];
        }
            break;
        case 4:
        {
            LCYProductController *productTVC = [[LCYProductController alloc]init];
            productTVC.title = title;
            [self.navigationController pushViewController:productTVC animated:YES];
        }
            break;
            
        default:
            break;
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    
    return cell;
}


#pragma mark - UICollectionViewDelegate
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        // 创建头部的轮播图
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        [headerView addSubview:self.cycleView];
        return headerView;
    }
    return nil;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LCYHomeDetailController *homeDetailTVC = [[LCYHomeDetailController alloc]init];
    
    LCYHomeModel *model = self.dataSource[indexPath.row];

    homeDetailTVC.type = [NSString stringWithFormat:@"%ld",model.type];
    
    [self.navigationController pushViewController:homeDetailTVC animated:YES];
    
    
    homeDetailTVC.navItemTitle = [NSString stringWithFormat:@"%@・%@",model.name,model.enname];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
